package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.weather.bean.WeaBean;
import com.google.gson.Gson;

public class SearchCityActivity extends lianwang implements View.OnClickListener{
    RelativeLayout searchBg;
    EditText searchEt;
    ImageView submitIv;
    GridView searchGv;
    ArrayAdapter<String> adapter;
    String city;
    String[]hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州",
            "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁","乌鲁木齐","贵阳","营口"};
    String url1 = "http://api.tianapi.com/txapi/tianqi/index?key=5fcc28098b884f606428663b7df174ae&city=";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_city);
        searchEt = findViewById(R.id.search_et);
        searchBg=findViewById(R.id.searchbg);
        submitIv = findViewById(R.id.search_iv);
        searchGv = findViewById(R.id.search_gv);
        adapter = new ArrayAdapter<>(this, R.layout.item_recommend_city, hotCitys);
        searchGv.setAdapter(adapter);
        searchGv.setEmptyView(findViewById(R.id.nadata));
        submitIv.setOnClickListener(this);
        setListener();
        searchBg.getBackground().setAlpha(150);

    }

    private void setListener() {
        searchGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                city = hotCitys[position];
                String url =url1+city;
                lianWang(url);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_iv:
                city = searchEt.getText().toString();
                if (city!=null&& city.length()>0) {
                    //判断是否有这个城市
                    String url = url1+city;
                    lianWang(url);
                }else {
                    Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onSuccess(String result) {
        WeaBean weaBean=new Gson().fromJson(result,WeaBean.class);
        if(weaBean.getCode()==200){
            Intent intent = new Intent(this, MainActivity.class);
            //通过intent开启一个activity，并将这个activity放至栈底或者清空栈后再把这个activity压进栈去
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.putExtra("city",city);
            startActivity(intent);
        }
        else{
            Toast.makeText(this,"未查找到该城市",Toast.LENGTH_SHORT).show();
        }
    }
    
}